﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace weiqijiance.油耗仪
{
    public class yhbass
    {
        private SerialPort comYH = new SerialPort();//油耗串口
        public float m_fYH=0;//每秒油耗值
        public float m_fYHZH=0;//油耗总和
        public float m_fXXLL=0;//流量
        public float m_fHC=0;//HC 
        public float m_fCO2=0;//CO2 
        public float m_fCO=0;//CO
        public float fwd = 0;
        public float fsd = 0;
        public float fdqy = 0;
        public void init()
        {

            iniclass objini = new iniclass();
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\安综检仪表配置.ini";
            string path1 = string.Format(@"{0}", path);
            objini.Ini(path1);
            string sSetCom = objini.ReadValue("油耗", "油耗串口设置").Trim();

            //sSetCom = ReadValue("串口设置", "油耗仪");
            if (sSetCom != "")
            {
                openCOM(sSetCom, comYH);
                comYH.ReadTimeout = 500;
                //   comYH.ReceivedBytesThreshold = 10;//达到50字节以后才开始触发串口
                comYH.DataReceived += comYH_DataReceived;
            }
        }

        public bool openCOM(string sSet1, SerialPort commex)//打开串口
        {

            string[] szbzlCOM = new string[5];
            if (sSet1.Length > 0)
                szbzlCOM = sSet1.Split(':');
            //打开串口
            commex.PortName = szbzlCOM[0];
            commex.BaudRate = Convert.ToInt32(szbzlCOM[1]);

            commex.NewLine = "/r/n";
            commex.RtsEnable = true;//根据实际情况吧。 
            commex.ReadTimeout = 300;//读取数据延时，避免读取不全数据

            if (szbzlCOM[3] == "0")
                commex.Parity = Parity.None;
            else
                commex.Parity = Parity.Even;
            // ////添加事件注册  
            //  comm.DataReceived += comm_Data;
            try
            {
                commex.Open();
            }
            catch (Exception)
            {
                //lzj20200706  MessageBox.Show(string.Format("{0}\n请检查电脑的串口故障", ex.Message));
                return false;
            }
            return true;
        }
        public void sendYHcom(byte cCmd2, byte cCmd4)//发送指令给油耗串口
        {
            byte[] cCmdBuf = new byte[4] { 0x00, 0x00, 0x00, 0x00 };

            cCmdBuf[0] = 0x07;
            cCmdBuf[1] = cCmd2;
            cCmdBuf[2] = 0x03;
            cCmdBuf[3] = cCmd4;//
            if (comYH.IsOpen)
            {
                Byte[] m_byte = new Byte[4];
                for (int i = 0; i < 4; i++)
                {
                    m_byte[i] = cCmdBuf[i];
                }
                comYH.Write(m_byte, 0, 4);
            }
        }

        //   油耗
        void comYH_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (comYH.IsOpen)
            {
                Thread.Sleep(500);
                int n = comYH.BytesToRead;//
                byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                comYH.Read(buf1, 0, n);
                if (n > 28 && buf1[1] == 0x70 && buf1[2] == 0x21)
                {
                    if (buf1[25] * 256 * 256 * 256 + buf1[26] * 256 * 256 + buf1[27] * 256 + buf1[28] >= 0 && buf1[25] * 256 * 256 * 256 + buf1[26] * 256 * 256 + buf1[27] * 256 + buf1[28] < 5000)
                    {
                        float.TryParse(string.Format("{0}", (float)(buf1[25] * 256 * 256 * 256 + buf1[26] * 256 * 256 + buf1[27] * 256 + buf1[28]) / 1000f), out m_fYH);

                        //float.TryParse(string.Format("{0}", buf1[29] * 256 * 256 * 256 + buf1[30] * 256 * 256 + buf1[31] * 256 + buf1[32]), out m_fYHZH);

                        float.TryParse(string.Format("{0}", buf1[11] * 256 + buf1[12]), out m_fXXLL);
                        float.TryParse(string.Format("{0}", buf1[7] * 256 + buf1[8]), out m_fHC);
                        float.TryParse(string.Format("{0}", (float)(buf1[5] * 256 + buf1[6]) / 100), out m_fCO2);
                        float.TryParse(string.Format("{0}", buf1[3] * 256 + buf1[4]), out m_fCO);
                    }
                }
                if (n > 28 && buf1[1] == 0x00 && buf1[2] == 0x1D)
                {
                    //if (buf1[25] * 256 * 256 * 256 + buf1[26] * 256 * 256 + buf1[27] * 256 + buf1[28] > 0 && buf1[25] * 256 * 256 * 256 + buf1[26] * 256 * 256 + buf1[27] * 256 + buf1[28] < 5000)
                    {
                        float.TryParse(string.Format("{0}", (float)(buf1[25] * 256 * 256 * 256 + buf1[26] * 256 * 256 + buf1[27] * 256 + buf1[28]) / 1000f), out m_fYH);

                        //float.TryParse(string.Format("{0}", buf1[29] * 256 * 256 * 256 + buf1[30] * 256 * 256 + buf1[31] * 256 + buf1[32]), out m_fYHZH);

                        float.TryParse(string.Format("{0}", buf1[15] * 256 + buf1[16]), out fwd);
                        float.TryParse(string.Format("{0}", buf1[17] * 256 + buf1[18]), out fdqy);
                        float.TryParse(string.Format("{0}", buf1[19] * 256 + buf1[20]), out fsd);
                        fwd = fwd / 10f;
                        fdqy = fdqy / 10f;
                        fsd = fsd / 10f;
                    }
                }
            }

        }
    }
}
